package com.yang.libs.ui

import android.util.Log
import android.view.View
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.xxx.lib.GlideApp
import com.xxx.lib.base.BasePresenterActivity
import com.xxx.lib.bean.EventBusBean
import com.xxx.lib.constant.Event
import com.xxx.lib.utils.PictureSelectorUtils
import com.xxx.lib.widget.HintDialog
import com.yang.libs.bean.MainBean
import com.yang.libs.databinding.ActivityMainBinding
import com.yang.libs.mvppresenter.MainPresenter
import com.yang.libs.mvpview.MainView
import org.greenrobot.eventbus.Subscribe

/**
 * 主页
 */
class MainActivity : BasePresenterActivity<MainView, MainPresenter>(), MainView,
    OnResultCallbackListener<LocalMedia> {

    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    override fun initContentView(): View? = binding.root

    override fun createPresenter(): MainPresenter = MainPresenter()

    override fun init() {
        title = "主页标题"
        binding.btOne.setOnClickListener {
            //请求业务接口
            getPresenter().getMallData()
        }
        binding.btTwo.setOnClickListener {
            //路由跳转
//            ARouter.getInstance().build(ARouterLink.LINK_LUANCH_ACTIVITY)
//                .navigation()

//            registerEventBus()
//            EventBus.getDefault().post(EventBusBean(Event.EVENT_REFRESH_HOME_FRAGMENT, "测试"))

//            WebViewActivity.startLoadLink(this, "https://wap.baidu.com", "哈哈")

            HintDialog(this).apply {
                setTitle("温馨提示")
                setButtonTextLeftRight("取消", "确定")
                setHint("确定退出登录？")
                show()
            }

        }

        binding.btThree.setOnClickListener {
//            PictureSelectorUtils.openCamera(this, this)
//            PictureSelectorUtils.openCameraCrop(this, 1F, 1F, this)
            PictureSelectorUtils.openGallerySingle(this, requestCode = 666)
        }
    }


    @Subscribe
    fun test(eventBusBean: EventBusBean) {
        if (eventBusBean.event == Event.EVENT_REFRESH_HOME_FRAGMENT) {
            Log.i("YangLog", Thread.currentThread().name)
            ToastUtils.showLong(eventBusBean.bean.toString())
        }
    }


    override fun getMainDataSuccess(bean: MainBean) {
        //请求业务接口成功
        binding.tvContent.text = GsonUtils.toJson(bean)
        GlideApp.with(this)
            .load("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png")
            .into(binding.ivImg)
    }

    override fun getMainDataFail() {
        //请求业务接口失败
    }

    override fun onResult(result: ArrayList<LocalMedia>?) {
    }

    override fun onCancel() {
    }
}
